﻿package mortal.game.view.common.guide.stepOperater
{
    import flash.geom.*;
    import flash.utils.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.guide.data.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.pack.*;
    import mortal.mvc.core.*;

    public class AutoGuideBackPackSteper extends AutoGuideStepBase implements IAutoGuideSteper
    {
        private var _itemData:ItemData;
        private var _point:Point;
        private var _win:PackModule;
        private var _timerId:int = -1;
        private var _guideItemCode:int = -1;

        public function AutoGuideBackPackSteper()
        {
            return;
        }// end function

        public function guide(param1:AutoGuideData, param2:Window = null) : void
        {
            _data = param1;
            this._guideItemCode = this.getItemCode(param1.guideTypeValue);
            var _loc_3:* = Cache.instance.pack.backPackCache.getItemByCode(this._guideItemCode);
            if (_loc_3 == null)
            {
                return;
            }
            if (!GameController.pack.isViewShow)
            {
                GameManager.instance.popupModuleType(ModuleType.Pack);
            }
            var _loc_4:* = GameController.pack.getTabIndex(this._guideItemCode);
            if (_loc_4 < 0)
            {
                return;
            }
            if (_working)
            {
                this.stop();
            }
            this._win = GameController.pack.view as PackModule;
            this._win.changeTabIndex(_loc_4);
            this._timerId = setTimeout(this.guideUse, 50);
            this._win.addEventListener(WindowEvent.CLOSE, this.winCloseHandler);
            super.setData(param1, this._win.highestContentContainer);
            return;
        }// end function

        private function winCloseHandler(event:WindowEvent) : void
        {
            if (this._timerId > 0)
            {
                clearTimeout(this._timerId);
                this._timerId = -1;
            }
            if (this._win != null)
            {
                this._win.removeEventListener(WindowEvent.CLOSE, this.winCloseHandler);
            }
            super.stopMe();
            Dispatcher.removeEventListener(EventName.PackClickedToUseItem, this.clickedUseItemHandler);
            if (GuideController.shouldResumeToTaskAfterGuide() && _data.filterName != "NoMainEndType")
            {
                GameController.guide.resumeToTask();
            }
            return;
        }// end function

        private function getItemCode(param1:String) : int
        {
            var _loc_4:String = null;
            var _loc_5:Array = null;
            if (param1.indexOf(",") < 0)
            {
                return parseInt(param1);
            }
            var _loc_2:* = param1.split(",");
            var _loc_3:* = Cache.instance.role.roleEntityInfo.entityInfo.career;
            for each (_loc_4 in _loc_2)
            {
                
                _loc_5 = _loc_4.split("#");
                if (int(_loc_5[1]) == _loc_3)
                {
                    return int(_loc_5[0]);
                }
            }
            return -1;
        }// end function

        private function guideUse() : void
        {
            this._timerId = -1;
            var _loc_1:* = GameController.pack.getAndAdaptPlace(this._guideItemCode);
            if (_loc_1 == null)
            {
                return;
            }
            var _loc_2:* = new Rectangle(_loc_1.x, _loc_1.y, 44, 44);
            if (_data.needMask)
            {
                showMaskByRect(this._win.highestContentContainer, _loc_2);
            }
            if (_data.needArrow)
            {
                showArrowByRect(this._win.highestContentContainer, _data, _loc_2);
            }
            if (_data.needLightMask)
            {
                showLightMaskByRect(this._win.highestContentContainer, _loc_2);
            }
            Dispatcher.addEventListener(EventName.PackClickedToUseItem, this.clickedUseItemHandler);
            return;
        }// end function

        private function clickedUseItemHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as ItemData;
            if (_loc_2 == null)
            {
                return;
            }
            if (_loc_2 != null && _loc_2.itemInfo.code == this._guideItemCode)
            {
                this.stop(_data);
            }
            return;
        }// end function

        override public function stop(param1:AutoGuideData = null) : void
        {
            super.stop(param1);
            Dispatcher.removeEventListener(EventName.PackClickedToUseItem, this.clickedUseItemHandler);
            if (this._timerId > 0)
            {
                clearTimeout(this._timerId);
                this._timerId = -1;
            }
            if (this._win != null)
            {
                this._win.removeEventListener(WindowEvent.CLOSE, this.winCloseHandler);
            }
            if (param1 != null)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideStepEnd, param1));
            }
            return;
        }// end function

    }
}
